/**
 * 字符串替换
 * @param str 要替换的字符串
 * @param are 参数顺序替换
 * @constructor
 */
const AreFormat = (str:string, ...are:string[]) => {
    if (are.length == 0) return str;
    let s:string = str;
    for (let i = 0; i < are.length; i++) {
        s = s.replace(new RegExp("\\{" + i + "\\}", "g"), are[i]);
    }
    return s;
};
/**
 * 字符串替换，对象名称替换
 * @param str 要替换的字符串
 * @param obj 替换的字符的对象
 * @returns {String}
 */
const ObjFormat = (str:string, obj:any) => {
    let s = str;
    for(let i in obj){
        if(obj[i] instanceof Object){
            s = ObjFormat(s,obj[i]);
            continue;
        }
        s = s.replace(new RegExp("\\{" + i + "\\}", "gi"), obj[i]);
    }
    return s;
};
/**
 * 获取字符串长度
 * @param str
 * @param cn   中文是否两字节
 * @returns {number}
 */
export const getStrLen = (str: string, cn: boolean = false) : number => {
    let chart: any;
    let strLength = 0;
    for (let i = 0; i < str.length; i++) {
        chart = str.charAt(i);
        strLength++;
        if (cn) {
            if (escape(chart).length > 4) {
                strLength++;
            }
        }
    }
    return strLength;
};
export const CompareVersion = (v1: string, v2: any) : number => {
    if (v2) {
        const v1Arr = v1.split('.');
        const v2Arr = v2.split('.');
        const len = Math.max(v1Arr.length, v2Arr.length);
        // 调整两个版本号位数相同
        while (v1Arr.length < len) {
            v1Arr.push('0')
        }
        while (v2Arr.length < len) {
            v2Arr.push('0')
        }
        // 循环判断每位的大小
        for (let i = 0; i < len; i++) {
            let num1: any = parseInt(v1Arr[i]);
            let num2: any = parseInt(v2Arr[i]);

            if (!isNaN(num1) && !isNaN(num2)) {
                if (num1 > num2) {
                    return 1;
                } else if (num1 < num2) {
                    return -1;
                }
            } else {
                if (v1Arr[i] > v2Arr[i]) {
                    return 1;
                } else if (v1Arr[i] < v2Arr[i]) {
                    return -1;
                }
            }
        }
        return 0;
    } else {
        return 1;
    }
    
}
export default {
    AreFormat,
    ObjFormat,
    getStrLen,
    CompareVersion
}
